package ysq.xyg.recovery.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.recovery.dao.PowerMemberDao;
import ysq.xyg.recovery.model.po.PowerMember;
import ysq.xyg.recovery.model.vo.PowerMemberVo;
import ysq.xyg.recovery.service.PowerMemberService;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.po.WebMember;
import ysq.xyg.web.service.WebMemberService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 回收金模块-权限用户表service实现
 *
 * @author congcong tao
 * @date 2019-9-18 15:15:22
 */
@Service("powerMemberService")
public class PowerMemberServiceImpl extends BaseServiceImpl<PowerMember> implements PowerMemberService {

    @Resource
    private WebMemberService webMemberService;
    @Resource
    private PowerMemberDao powerMemberDao;

    private PowerMember powerMember;

    @Autowired
    public void setBaseDao(PowerMemberDao powerMemberDao) {
        this.baseDao = powerMemberDao;
    }

    /**
     * 分页查询
     *
     * @param powerMemberVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-9-18 15:15:22
     */
    @Override
    public ReturnEntity page(PowerMemberVo powerMemberVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<PowerMember> list = acceptList(powerMemberVo);
        PageInfo<PowerMember> page=new PageInfo<PowerMember>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param powerMemberVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-9-18 15:15:22
     */
    @Override
    public ReturnEntity addOrUpdate(PowerMemberVo powerMemberVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(powerMemberVo.getId())){
            powerMember=new PowerMember(currentUser);
            isUpate=false;
        }else{
            powerMember=selectByPrimaryKey(powerMemberVo.getId());
            if(ObjectUtil.isNull(powerMember)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            powerMember.setUpdateTime(new Date());
            powerMember.setUpdateUser(currentUser.getId());
        }
        powerMember.setItemMemberId(powerMemberVo.getItemMemberId());
        powerMember.setOrderMemberId(powerMemberVo.getOrderMemberId());
        powerMember.setWithdrawMemberId(powerMemberVo.getWithdrawMemberId());
        powerMember.setCardMemberId(powerMemberVo.getCardMemberId());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(powerMember);
        }else{
            num=insert(powerMember);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-9-18 15:15:22
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        PowerMember powerMember=selectByPrimaryKey(id);
        if(powerMember!=null){
            powerMember.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            powerMember.setUpdateTime(new Date());
            powerMember.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(powerMember);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param powerMemberVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-9-18 15:15:22
     */
    @Override
    public List<PowerMember> acceptList(PowerMemberVo powerMemberVo){
        Example example = new Example(PowerMember.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(powerMemberVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",powerMemberVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return powerMemberDao.selectByExample(example);
    }

    @Override
    public String selectOpenIdByType(String type) {
        List<PowerMember> powerMembers = acceptList(new PowerMemberVo());
        if (powerMembers != null && powerMembers.size() > 0) {
            PowerMember powerMember = powerMembers.get(0);
            String memberId = "";
            // 回收权限，1=预约2=兑换3=提现4=名片
            if (StringUtils.equals(type, BaseConstant.POWER_MEMBER_ORDER)) {
                memberId = powerMember.getOrderMemberId();
            } else if (StringUtils.equals(type, BaseConstant.POWER_MEMBER_ITEM)) {
                memberId = powerMember.getItemMemberId();
            } else if (StringUtils.equals(type, BaseConstant.POWER_MEMBER_WITHDRAW)) {
                memberId = powerMember.getWithdrawMemberId();
            } else if (StringUtils.equals(type, BaseConstant.POWER_MEMBER_CARD)) {
                memberId = powerMember.getCardMemberId();
            }
            WebMember webMember = webMemberService.selectByPrimaryKey(memberId);
            return webMember.getOpenid();
        }
        return null;
    }
}
